{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6db7e326",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(70000,)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import fetch_openml\n",
    "import numpy as np\n",
    "\n",
    "# 读取数据集\n",
    "mnist = fetch_openml('mnist_784', parser='auto')\n",
    "# print(mnist)\n",
    "\n",
    "# 取特征数据和标签\n",
    "X, y = mnist[\"data\"], mnist[\"target\"]\n",
    "# print(X.shape)\n",
    "print(y.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8cb88620-8c0b-4bc0-8a79-15305bc07615",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14091    2\n",
      "26862    5\n",
      "56684    9\n",
      "17945    2\n",
      "45200    7\n",
      "51704    7\n",
      "22462    1\n",
      "43340    7\n",
      "49559    1\n",
      "17362    8\n",
      "Name: class, dtype: int32\n"
     ]
    }
   ],
   "source": [
    "# y值转int\n",
    "y = y.astype(int)\n",
    "# 数据集切分，拆分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]\n",
    "# print(X_train.shape)\n",
    "# print(y_train.shape)\n",
    "# print(X_train)\n",
    "# 洗牌操作\n",
    "shuffle_index = np.random.permutation(60000)\n",
    "# print(shuffle_index)\n",
    "# print(X_train.columns)\n",
    "X_train = X_train.loc[shuffle_index]  # 使用loc根据索引标签取值\n",
    "y_train = y_train[shuffle_index]  # series直接传入索引值即可\n",
    "\n",
    "print(y_train[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fe97c061-9834-4523-9872-9e4755585398",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14091    False\n",
      "26862     True\n",
      "56684    False\n",
      "17945    False\n",
      "45200    False\n",
      "51704    False\n",
      "22462    False\n",
      "43340    False\n",
      "49559    False\n",
      "17362    False\n",
      "Name: class, dtype: bool\n",
      "预测值\n",
      " [False False False False False False False  True False False]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\app\\python38\\lib\\site-packages\\sklearn\\linear_model\\_stochastic_gradient.py:713: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "# 分类,等于5和不等于5两类\n",
    "y_train_5 = (y_train == 5)\n",
    "y_test_5 = (y_test == 5)\n",
    "\n",
    "print(y_train_5[:10])\n",
    "\n",
    "# 基于随机梯度下降算法的分类器\n",
    "from sklearn.linear_model import SGDClassifier\n",
    "\n",
    "# max_iter：最大迭代次数，random_state：用于控制随机数生成器的种子\n",
    "sgd_clf = SGDClassifier(max_iter=100, random_state=42)\n",
    "# 训练\n",
    "sgd_clf.fit(X_train, y_train_5)\n",
    "# 预测\n",
    "predict = sgd_clf.predict(X_test[:10])\n",
    "print(\"预测值\\n\", predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6df42e7f-17f3-4449-919f-eab19bbd4309",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\app\\python38\\lib\\site-packages\\sklearn\\linear_model\\_stochastic_gradient.py:713: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.\n",
      "  warnings.warn(\n",
      "D:\\app\\python38\\lib\\site-packages\\sklearn\\linear_model\\_stochastic_gradient.py:713: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-43818.44809381  12101.57837957 -80591.96390823 -52280.16228069\n",
      " -35374.98184951 -43414.30152941 -30952.44887712 -14927.41113029\n",
      " -41560.24208076 -46865.9944691 ]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\app\\python38\\lib\\site-packages\\sklearn\\linear_model\\_stochastic_gradient.py:713: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import cross_val_predict\n",
    "# 生成阈值，并根据阈值生成精度和召回率\n",
    "y_scores = cross_val_predict(sgd_clf, X_train, y_train_5, cv=3,\n",
    "                             method=\"decision_function\")\n",
    "# 打印得分前十个\n",
    "print(y_scores[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "efa84e97-420d-46f1-ab42-1bec0dd97f5c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-220200.11508551 -199681.84952233 -186132.89590649 ...   51126.08950791\n",
      "   54642.80286271   59320.65041633]\n",
      "[0.09035    0.09035151 0.09035301 ... 1.         1.         1.        ]\n",
      "[1.00000000e+00 1.00000000e+00 1.00000000e+00 ... 3.68935621e-04\n",
      " 1.84467810e-04 0.00000000e+00]\n"
     ]
    }
   ],
   "source": [
    "# 引入准确率，召回率方法\n",
    "from sklearn.metrics import precision_recall_curve\n",
    "precission,recalls,thresholds = precision_recall_curve(y_train_5,y_scores)\n",
    "# 阈值\n",
    "print(thresholds)\n",
    "# 精度\n",
    "print(precisions)\n",
    "# 召回率\n",
    "print(recalls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "500bae7a-edee-410a-b0e8-d73e821e6b95",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 随着精度和召回率，在不同阈值的时候变化，需要综合考虑设置\n",
    "#ROC评估方法\n",
    "# tpr:准确率\n",
    "# fpr:失败率 \n",
    "# 期望是tpr值越大越好，fpr越小越好abs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4e980009-f849-4e93-a144-052bb8c4f74b",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'precisions' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[8], line 18\u001b[0m\n\u001b[0;32m     15\u001b[0m     plt\u001b[38;5;241m.\u001b[39mylim([\u001b[38;5;241m0\u001b[39m,\u001b[38;5;241m1\u001b[39m])\n\u001b[0;32m     17\u001b[0m plt\u001b[38;5;241m.\u001b[39mfigure(figsize\u001b[38;5;241m=\u001b[39m(\u001b[38;5;241m8\u001b[39m, \u001b[38;5;241m4\u001b[39m))\n\u001b[1;32m---> 18\u001b[0m plot_precision_recall_vs_threshold(\u001b[43mprecisions\u001b[49m,recalls,thresholds)\n\u001b[0;32m     19\u001b[0m plt\u001b[38;5;241m.\u001b[39mxlim([\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m700000\u001b[39m, \u001b[38;5;241m700000\u001b[39m])\n\u001b[0;32m     20\u001b[0m plt\u001b[38;5;241m.\u001b[39mshow()\n",
      "\u001b[1;31mNameError\u001b[0m: name 'precisions' is not defined"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x400 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "def plot_precision_recall_vs_threshold(precisions,recalls,thresholds):\n",
    "    plt.plot(thresholds,\n",
    "             precisions[:-1],\n",
    "            \"b--\",\n",
    "            label=\"Precision\")\n",
    "    \n",
    "    plt.plot(thresholds,\n",
    "             recalls[:-1],\n",
    "            \"g-\",\n",
    "            label=\"Recall\")\n",
    "    plt.xlabel(\"Threshold\",fontsize=16)\n",
    "    plt.legend(loc=\"upper left\",fontsize=16)\n",
    "    plt.ylim([0,1])\n",
    "    \n",
    "plt.figure(figsize=(8, 4))\n",
    "plot_precision_recall_vs_threshold(precisions,recalls,thresholds)\n",
    "plt.xlim([-700000, 700000])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9dd14ffc-4570-4e7f-8f56-8a53d7872163",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
